﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FILSTARTUPS_MOBILE.Models;
using FILSTARTUPS_MOBILE.ViewModels;
using FILSTARTUPS_MOBILE.Helpers;

namespace FILSTARTUPS_MOBILE.Controllers
{
    public class ForumController : Controller
    {
        //
        // GET: /Forum/
        FilStatUpsEntities mobile_connect = new FilStatUpsEntities();

        public ActionResult Index()
        {
            /*
             * LOAD MAIN TOPICS (API)
             * var forum = FilStartUps.GETFORUMTOPIC();
             * return View(forum);
             * */
            try
            {
                var forum = new ForumPage();
                forum.forum = FilStartUps.GetForumTopic();
                forum.PageNav = FilStartUps.GetPageNav();

                if (forum != null)
                {
                    return View(forum);
                }
                else
                {
                    //VALIDTION
                    return null;
                }
            }
            catch
            {
                //TODO VALIDATION
                return null;
            }
        }

        public ActionResult ViewThread(int forumidFK)
        {
            /* COUNT(VIEW);
             * LATEST POST/REPLY GETFORUMTHREAD.FIRSTORDEFAULT(); ORDERBY.ASC/DESC
             * ViewBag or Service =>Title
             * */
            var ViewThread_ = new ViewThreadPost();

            int forumid = Convert.ToInt32(Request.QueryString["forumidFK"].Trim());

            ViewThread_.PageNav = FilStartUps.GetPageNav();

            ViewThread_.forum = mobile_connect.ForumThreads
                                .Where(f => f.forumidFK.Equals(forumid))
                                .OrderBy(f=>f.forumthreadidPK).ToList().Take(10);

            ViewBag.forumidFK = forumid;

            if (ViewThread_ != null)
            {
                return View(ViewThread_);
            }
            else
            {
                return View();
            }
            
        }
        //@Html.ActionLink("Create", "CreateThread", "Forum", new { id = var.forumidPK })
        public ActionResult CreateThread(int id)
        {
            ViewBag.forumidFK = id;
            return View();
        }

        //Check if (form["forumdescription"].trim() == String.Empty) return View(); 
        // Html.BeginForm("CreateThread", "Forum", FormMethod.Post , new { id = var.forumidPK })
        [HttpPost]
        public ActionResult CreateThread(ForumThread forumthread, FormCollection form)
        {
            /*
             * FORUMIDPK = > API
             * AJAX FOR PREVIEW
             * */
            var user = mobile_connect.Members.Where(m => m.emailaddress.Equals(User.Identity.Name)).SingleOrDefault();

        

                if (form["forumdescription"].Trim() == string.Empty)
                {
                    TempData["message"] = string.Format("Description is empty");
                    return View(forumthread);
                }

                if (ModelState.IsValid)
                {
                    forumthread.forumidFK = Convert.ToInt32(form["forumidFK"].Trim());
                    forumthread.memberidFK=user.memberidPK;
                    forumthread.datecreated = DateTime.Now;
                    mobile_connect.ForumThreads.Add(forumthread);
                    mobile_connect.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
          
           
        }

        public ActionResult ViewTopic(int id)
        {
            /*
             * LOAD => FORUMTHREAD.INCLUDE("FORUMREPLY") 
             * POSTED BY. 3 Models
             * CSS
             * */
            var Thread_Info = new ForumThread_Info();

            Thread_Info.PageNav = FilStartUps.GetPageNav();

            Thread_Info.ForumThreadMembers = (from m in mobile_connect.Members
                                              join t in mobile_connect.ForumThreads
                                              on m.memberidPK equals t.memberidFK
                                              where t.forumthreadidPK.Equals(id)
                                              select new ForumThreadMember
                                              {
                                                  dateposted=t.datecreated,
                                                  forumthreadidPK=t.forumthreadidPK,
                                                  forumdescription=t.forumdescription,
                                                  forumtitle=t.forumtitle,
                                                  membername=m.emailaddress

                                              }).SingleOrDefault();
            Thread_Info.ForumReplies = (from m in mobile_connect.Members
                                        join r in mobile_connect.ForumReplies
                                        on m.memberidPK equals r.memberidFK
                                        where r.forumthreadidFK.Equals(id)
                                        select new ForumReplies
                                        {
                                            datereplied=r.datereplied,
                                            forumreplyidPK=r.forumreplyidPK,
                                            forumthreadidFK=id,
                                            membername=m.emailaddress,
                                            replydescription=r.replydescription

                                        }).Take(100).ToList();



            //var viewthread = mobile_connect.ForumThreads
            //                        .Where(t => t.forumthreadidPK.Equals(id)).SingleOrDefault();

            if (Thread_Info != null)
            {
                return View(Thread_Info);
            }
            else
            {
                //ERROR
                return View();
            }
        }

        public ActionResult ReplyTopic(int id)
        {
            ViewBag.forumthreadidPK = id;
            return View();
        }

        //VALIDATION
        [HttpPost]
        public ActionResult ReplyTopic(ForumReply forumreply, FormCollection form )
        {
            var user_ =mobile_connect.Members.Where(m=>m.emailaddress.Contains(User.Identity.Name)).SingleOrDefault();
            try
            {
                if (form["replydescription"].Trim() == string.Empty)
                {
                    TempData["message"] = string.Format("Description is required");
                    return View(forumreply);
                }
                if (ModelState.IsValid)
                {
                    forumreply.forumthreadidFK = Convert.ToInt32(form["forumidthreadFK"].Trim());
                    forumreply.datereplied = DateTime.Now;
                    forumreply.memberidFK = user_.memberidPK;
                    mobile_connect.ForumReplies.Add(forumreply);
                    mobile_connect.SaveChanges();
                    return RedirectToAction("ViewTopic", new { id=forumreply.forumthreadidFK });
                }
                else
                {
                    return View();
                }

            }
            catch
            {
                return View();
            }
        }


        public ActionResult Report(int forumthreadidFK, int forumreplyidFK)
        {
            //ViewBag => id  or Hidden type
            return View();
        }

        [HttpPost]
        public ActionResult Report(ForumReport forumreport)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    mobile_connect.ForumReports.Add(forumreport);
                    mobile_connect.SaveChanges();
                    return View();
                }
                else
                {
                    return View(forumreport);
                }
            }
            catch
            {
                return View();
            }
        }


        //OUTPUT Save == X
        public PartialViewResult Preview()
        {
            return PartialView();
        }
    }
}
